package com.woniuxy.mesboot.security;

import com.woniuxy.mesboot.entity.SysUser;
import com.woniuxy.mesboot.service.MenuService;
import com.woniuxy.mesboot.service.UserService;
import com.woniuxy.mesboot.vo.MenuVO;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/30 11:11
 * @description：TODO
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser user = userService.selectByUsername(username);
        if (user.getStatus() == 1) {
            return null;
        }
        List<MenuVO> menuVOS = menuService.selectMenuByName(username, 0);
        LoginUser loginUser = new LoginUser();
        loginUser.setUser(user);
        loginUser.setMenuVOS(menuVOS);
        loginUser.setStatus(user.getStatus());
        return loginUser;
    }
}
